package io.wen.scala

import scala.io.StdIn
import scala.util.Random

class User(name: String, score: Int = 0) extends GamePlayer(name, score){
  override def showFirst(show: Int): Unit = show match {
    case 1 => println(name + "出剪刀")
    case 2 => println(name + "出石头")
    case 3 => println(name + "出布")
    case _ => println(name + "输入不符合规范，默认出布")
  }
}

class Computer(name: String, score: Int = 0) extends GamePlayer(name, score) {
  override def showFirst(show: Int): Unit = show match {
    case 1 => println(name + "出剪刀")
    case 2 => println(name + "出石头")
    case 3 => println(name + "出布")
  }
}

abstract class GamePlayer(val name: String, var score: Int) {
  var winCount: Int = _   // 胜局次数
  var tieCount: Int = _   // 和局次数
  var loseCount: Int = _  // 负局次数

  def showFirst(show: Int): Unit

  def incrWin(): Unit = {
    winCount += 1
    score += 2
  }

  def incrTie(): Unit = {
    tieCount += 1
    score += 1
  }
  def incrLose(): Unit = {
    loseCount += 1
  }
}

class FingerGuessingGame(val player1: User, val player2: Computer, var playCount: Int = 0) {

  /**
    * 判断胜负，统计信息
    * 1.剪刀  2.石头  3.布
    * diff为0，和局
    * diff为1或-2，前者胜
    * diff为-1或2，前者负
    */
  def judge(player1Finger: Int, player2Finger: Int): Unit = {
    assert(player2Finger >= 1 && player2Finger <= 3)

    player1.showFirst(player1Finger)
    player2.showFirst(player2Finger)

    var finger1 = player1Finger
    var finger2 = player2Finger

    if (finger1 <= 1 || finger1 >= 3) finger1 = 3

    val diff: Int = finger1 - finger2
    if (diff == 0) {
      player1.incrTie()
      player2.incrTie()
      println("和局")
    } else if (diff == 1 || diff == -2) {
      player1.incrWin()
      player2.incrLose()
      println(s"${player1.name}胜，${player2.name}负")
    } else if (diff == -1 || diff == 2) {
      player1.incrLose()
      player2.incrWin()
      println(s"${player2.name}胜，${player1.name}负")
    }
  }

  /**
    * 展示统计结果
    */
  def showResult(): Unit = {
    println("----------------------------------")
    println(s"${player1.name} VS ${player2.name}")
    println(s"对战次数$playCount")

    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"${player1.name}\t${player1.score}\t${player1.winCount}\t${player1.tieCount}\t${player1.loseCount}")
    println(s"${player2.name}\t${player2.score}\t${player2.winCount}\t${player2.tieCount}\t${player2.loseCount}")
  }
}


object FingerGuessingGame {
  def main(args: Array[String]): Unit = {
    println(
      """
        |***************************************
        |***************猜拳游戏****************
        |***************************************
      """.stripMargin)

    println("请选择对战角色：(1.刘备  2.关羽  3.张飞)")
    val chooseId = StdIn.readInt()
    val computer = choosePlayer(chooseId)
    val user = new User("游客")

    val game: FingerGuessingGame = new FingerGuessingGame(user, computer)

    println(s"你选择了与${computer.name}对战")
    println("要开始么？y/n")
    var doNext = "y".equalsIgnoreCase(StdIn.readLine())
    while (doNext) {
      game.playCount += 1
      println("请出拳：(1.剪刀  2.石头  3.布)")
      val userFinger = StdIn.readInt()
      val computerFinger = Random.nextInt(3) + 1

      game.judge(userFinger, computerFinger)
      println("是否开始下一轮？y/n")
      doNext = "y".equalsIgnoreCase(StdIn.readLine())
    }
    println("退出游戏！")
    game.showResult()
  }

  /**
    * 选择对手
    */
  def choosePlayer(id: Int): Computer = id match {
    case 1 =>  new Computer("刘备")
    case 2 =>  new Computer("关羽")
    case 3 =>  new Computer("张飞")
    case _ =>  new Computer("瓦力")
  }
}


